import { _decorator, Component, EventKeyboard, input, Input, instantiate, KeyCode, Node, Prefab, UITransform, Vec2, Vec3 } from 'cc';
import { ImageArranger } from './ImageArranger';

const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {

    @property(Node)
    bulletPrarent: Node = null;

    @property(Node)
    bulletPrarent1: Node = null;

    @property(Node)
    bulletPostion: Node = null;

    @property(Prefab)
    bullet1Prefab: Prefab = null;

    @property(Prefab)
    bullet2Prefab: Prefab = null;

    @property(Prefab)
    bullet3Prefab: Prefab = null;

    @property(Prefab)
    bullet4Prefab: Prefab = null;

    @property(Prefab)
    bullet5Prefab: Prefab = null;

    @property
    bulletSize: Vec2 = new Vec2(200, 200);

    @property
    spacing: number = 10;

    @property
    speed: number = 5;

    private startY: number;
    private contentHeight: number;
    // 控制滚动状态
    private isPaused: boolean = true;


    onLoad() {
        const bulletPrefabs = [this.bullet1Prefab, this.bullet2Prefab, this.bullet3Prefab, this.bullet4Prefab, this.bullet5Prefab];
        const beginX: number = -460;
        const beginY: number = 270;
        // 监听键盘事件
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        bulletPrefabs.forEach((prefab, index) => {
            // 创建并添加子弹到父节点
            const bullet = instantiate(prefab);
            const bulletClone = instantiate(prefab);

            this.bulletPrarent.addChild(bullet);
            this.bulletPrarent1.addChild(bulletClone);

            // 设置子弹的大小
            const uiTransform = bullet.getComponent(UITransform);
            if (uiTransform) {
                uiTransform.setContentSize(this.bulletSize.x, this.bulletSize.y);
            }

            // 计算并设置位置
            const bulletWidth = 200;
            const positionX = beginX + (bulletWidth + this.spacing) * index;
            const positionY = beginY;

            bullet.setPosition(new Vec3(positionX, positionY, this.bulletPostion.position.z));
            bulletClone.setPosition(new Vec3(positionX, positionY, this.bulletPostion.position.z)); // 设置克隆的位置

            // 更新起始 Y 坐标
            this.startY = this.bulletPostion.position.y;

            // 处理 ImageContain 节点
            this.updateContentHeight(bullet);
        });
    }

    // 更新内容高度的辅助方法
    private updateContentHeight(bullet: Node) {
        const imageContainNode = bullet.getChildByName('ImageContain');
        if (imageContainNode) {
            const imageComponent = imageContainNode.getComponent(ImageArranger);
            if (imageComponent) {
                const container = imageComponent.container;
                this.contentHeight = container.children.length * 210; // 假设每个子节点高度为 210
            }
        }
    }


    // 开始时执行的代码
    start() {
        // 初始化背景位置
        this.bulletPrarent.setPosition(0, 0, 0);
        // bg02 在 bg01 之上
        this.bulletPrarent1.setPosition(0, this.contentHeight, 0);
        console.log("初始位置", this.bulletPrarent.position.y, this.bulletPrarent1.position.y);
    }

    update(deltaTime: number) {
        if (!this.isPaused) {
            // 移动
            this.moveBackground(this.bulletPrarent, deltaTime);
            this.moveBackground(this.bulletPrarent1, deltaTime);
            console.log("变化位置", this.bulletPrarent.position.y, this.bulletPrarent1.position.y,);
        }
    }

    private moveBackground(bg: Node, deltaTime: number) {
        // 得到当前的位置
        let position = bg.position.clone(); // 克隆当前的位置
        // 更新 y 位置
        position.y -= this.speed * deltaTime;

        // 检查是否需要重置位置
        if (position.y <= -(this.contentHeight)) {
            // 重置到上方
            position.y += this.contentHeight * 2;
        }

        // 设置新的位置
        bg.setPosition(position);
    }
    /// 监听键盘空格输入
    onKeyDown(event: EventKeyboard) {
        switch (event. keyCode) { 
            case KeyCode.SPACE:
                console.log('Press a key');
                // 切换暂停状态
                this.isPaused = !this.isPaused;
                break;
        }
    }

    // 开始动画
    startRoll (){
        this.isPaused = false;
    }

    // 结束动画
    endRool(){
        this.isPaused = true;
    }


    onDestroy() {
        // 移除键盘事件监听
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    }
}